package cn.com.czpb.client.module.personal.acticity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.ToolBarActivityWhite;
import cn.com.czpb.client.module.personal.adapter.CodeFragmentadapter;
import cn.com.czpb.client.module.personal.fragment.InputCodeFragment;
import cn.com.czpb.client.module.personal.fragment.InviteFragment;


public class MyShareActivity extends ToolBarActivityWhite {
    public static String SHOW="MyShareActivity.show";
    @BindView(R.id.inviterl)
    RelativeLayout inviterl;
    @BindView(R.id.inputcoderl)
    RelativeLayout inputcoderl;
    @BindView(R.id.share_img_1)
    ImageView share_img_1;
    @BindView(R.id.share_img_2)
    ImageView share_img_2;
    @BindView(R.id.myscrollimg1)
    ImageView myscrollimg1;
//    @BindView(R.id.myscrollimg2)
//    ImageView myscrollimg2;
    @BindView(R.id.invitepager)
    ViewPager invitepager;
    CodeFragmentadapter adapter;
    List<Fragment> list;
    LoginQuitBR br = new LoginQuitBR();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<>();
        adapter = new CodeFragmentadapter(getSupportFragmentManager(),list,activity);
        invitepager.setAdapter(adapter);
        InviteFragment invitefragment = InviteFragment.newInstance();
        InputCodeFragment inputcodefragment = InputCodeFragment.newInstance();
        list.add(invitefragment);
        list.add(inputcodefragment);
        initbr();
        initViewpager();
        showinvitepage();
    }

    @Override
    protected void onRestart() {
        list.clear();
        list.add(InviteFragment.newInstance());
        list.add(InputCodeFragment.newInstance());
        adapter.notifyDataSetChanged();
        super.onRestart();
    }

    private void initViewpager() {
        invitepager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 0){
                    myscrollimg1.layout(positionOffsetPixels/2,0,positionOffsetPixels/2+myscrollimg1.getWidth(),myscrollimg1.getHeight());
                }
//                Log.e("MyShareActivity", "onPageScrolled------>position："+position+"\nonPageScrolled------>positionOffset:"+positionOffset+"\nonPageScrolled------>positionOffsetPixels:"+positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    showinvitepage();
                }else if(position == 1){
                    showinputcodepage();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initbr() {
        br = new LoginQuitBR();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SHOW);
        activity.registerReceiver(br, filter);
    }

    public class LoginQuitBR extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(SHOW)) {
                //预留接收注销广播
            }
        }
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_my_share;
    }
    @OnClick({
        R.id.inviterl,R.id.inputcoderl,
    })
    public void showpage(View view){
        switch (view.getId()){
            case R.id.inviterl:
                invitepager.setCurrentItem(0);
                showinvitepage();
                break;
            case R.id.inputcoderl:
                invitepager.setCurrentItem(1);
                showinputcodepage();
                break;
        }
    }

    private void showinvitepage() {
//        myscrollimg.scrollTo(0,myscrollimg.);
//        myscrollimg1.setVisibility(View.VISIBLE);
//        myscrollimg2.setVisibility(View.GONE);
        inviterl.setSelected(true);
        inputcoderl.setSelected(false);
        share_img_1.setVisibility(View.VISIBLE);
        share_img_2.setVisibility(View.GONE);
    }

    private void showinputcodepage() {
//        myscrollimg.scrollTo(x,y);
//        myscrollimg1.setVisibility(View.GONE);
//        myscrollimg2.setVisibility(View.VISIBLE);
        inviterl.setSelected(false);
        inputcoderl.setSelected(true);
        share_img_1.setVisibility(View.GONE);
        share_img_2.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        if(br != null){
            activity.unregisterReceiver(br);
        }
        super.onDestroy();
    }
}
